Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-async-storage

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-async-storage

Its a mock of react-native AsyncStorage for jest tests

  • 2.9.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
increased by15.44%
Maintainers
1
Weekly downloads
 
Created
Source

Mock AsyncStorage for react-native

Its a mock of react-native AsyncStorage for jest tests

ES modules

From 2.9.x version, added esmodules support.

I suggest to use this library with setupFilesAfterEnv configuration. Its more stable and during my tests its working as expected.

Install

NPM

  • Install: npm install --save mock-async-storage
  • Import: import MockAsyncStorage from 'mock-async-storage'

Quick Jest Example

// jest.config.js
module.exports = {
  setupFilesAfterEnv: [
    '<rootDir>/setup-tests.js',
  ],
};
// setup-tests.js
import MockAsyncStorage from 'mock-async-storage';

const mockImpl = new MockAsyncStorage();
jest.mock('@react-native-community/async-storage', () => mockImpl);

@react-native-async-storage/async-storage

import MockAsyncStorage from "mock-async-storage";

jest.mock(
  "@react-native-async-storage/async-storage",
  () => new MockAsyncStorage()
);

mock-async-storage@2.x.x

Whats the main difference?

I removed the jest specific part from the mock lib. In the next version the mocking method is not predefined. You can use any kind of library (sinon) for use this mock.

Usage

Manual mocks

I suggest to use jest manual mocks Jest Manual Mocks For demonstrate this solution you can find an example in examples folder.

Another mocking solution

import MockAsyncStorage from 'mock-async-storage';
// or import { mock, release } from 'mock-async-storage';
// mock();
// release();

const mock = () => {
  const mockImpl = new MockAsyncStorage()
  jest.mock('AsyncStorage', () => mockImpl)
}

const release = () => jest.unmock('AsyncStorage')

mock();

// For unmock
mockStorage.release();

Working example:

import 'react-native';
import MockAsyncStorage from 'mock-async-storage'
import React from 'react';
import Index from '../index.android.js';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

const mock = () => {
  const mockImpl = new MockAsyncStorage()
  jest.mock('AsyncStorage', () => mockImpl)
}

mock();

import { AsyncStorage as storage } from 'react-native'

it('renders correctly', () => {
  const tree = renderer.create(
    <Index />
  );
});

it('Mock Async Storage working', async () => {
  await storage.setItem('myKey', 'myValue')
  const value = await storage.getItem('myKey')
  expect(value).toBe('myValue')
})

mock-async-storage@1.x.x

Usage

In your test codes:

const mockStorage = require('mock-async-storage');
// or import { mock, release } from 'mock-async-storage';
// mock();
// release();

// For mock AsyncStorage
mockStorage.mock();

// For unmock
mockStorage.release();

Working example:

import 'react-native';
import { mock, release } from 'mock-async-storage'
import React from 'react';
import Index from '../index.android.js';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

mock()

import { AsyncStorage as storage } from 'react-native'

it('renders correctly', () => {
  const tree = renderer.create(
    <Index />
  );
});

it('Mock Async Storage working', async () => {
  await storage.setItem('myKey', 'myValue')
  const value = await storage.getItem('myKey')
  expect(value).toBe('myValue')
})

Keywords

FAQs

Package last updated on 22 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc